package com.woniuxy.rabbit.kill.controller;

import com.woniuxy.rabbit.kill.rabbit.KillProductor;
import com.woniuxy.rabbit.kill.util.ResponseResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * description: KillCtroller <br>
 * date: 2022/1/15 3:18 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

@RestController
public class KillCtroller {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private KillProductor killProductor;

    @GetMapping("/kill")
    public ResponseResult kill(String userName) {

        ResponseResult responseResult = new ResponseResult();

        //1、查询库存
        int num = (int) redisTemplate.opsForValue().get("num");

        //2、判断库存
        if (num <= 0) {
            //库存不足
            responseResult.setCode(200);
            responseResult.setMessage("商品已经被秒杀了！");
            return responseResult;
        }
        //3、库存不足，将秒杀的消息发送给消息队列
        //异步操作  子线程
        killProductor.send(userName);

        //当前请求已经结束了 request
        responseResult.setCode(200);
        responseResult.setMessage("正在为你抢购中，请耐心等待！");
        return responseResult;
    }

}
